package io.renren.common.utils;

import io.renren.modules.app.entity.SysAreaEntity;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @description: TreeBuilding
 * @date:
 */
public class TreeBuilding {

//    /**
//     * 将id-pid转为tree结构，单一顶层节点，顶层节点的id和pid不能相同
//     * @param pidList id-pid对象的列表
//     * @return tree结构对象
//     */
//    public PmsProductCategory buildTreeSinge(List<PmsProductCategory> pidList){
//        //以pid为Key进行分组存入Map
//        Map<Long,List<PmsProductCategory>> pidListMap =
//                pidList.stream().collect(Collectors.groupingBy(PmsProductCategory::getParentId));
//        pidList.stream().forEach(item->item.setChildren(pidListMap.get(item.getId())));
//        //取出顶层节点的对象，本例顶层节点的"PID"为0,注意是PID
//        return pidListMap.get(0).get(0);
//    }


    /**
     * 返回值改为List
     */
    public List<SysAreaEntity> buildTree(List<SysAreaEntity> pidList){
        Map<Integer,List<SysAreaEntity>> pidListMap =
                pidList.parallelStream().collect(Collectors.groupingBy(SysAreaEntity::getPid));
        pidList.forEach(item->item.setChildren(pidListMap.get(item.getId())));
        //返回结果也改为返回顶层节点的list
        return pidListMap.get(0);
    }

}
